package com.abc.cloud.controller;

import com.abc.cloud.common.utils.Result;
import com.abc.cloud.common.utils.UserHolder;
import com.abc.cloud.service.EmailService;
import com.abc.cloud.vo.mail.MailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "邮箱接口")
@RestController
@RequestMapping("/email")
public class EmailController {

    @Autowired
    private EmailService emailService;

    @ApiOperation("发送验证码（通用）")
    @PostMapping("/sendCode")
    public Result sendCode(@RequestBody MailVo mailVo){
        String mail = mailVo.getEmail();
        String username = UserHolder.getUser().getUsername();
        Boolean flag = emailService.sendCode(mail, username);

        if (flag) return Result.success(null, "发送邮件成功");
        else return Result.fail(null, "邮件已发送，请勿重复");
    }
}
